﻿using UnityEngine;
using System.Collections.Generic;
using TTGame;

/*
 * UISelectorInfo
 * 选择目标阶段时，UI上摇杆滑动得到的目标选择相关信息。
 *
 * 主要为了PuppetSkillCircle和UI摇杆之间去耦合。
 * UI摇杆设置数据，UISelectorInfo内会处理数据，然后PuppetSkillCircle取值绘制技能圈。
 *
 *
 * 依赖：
 * 无
 *
 * ！不写重复代码！
 * @author TTun
 * @date 7/18/2017 10:38:52 AM
 */
namespace TTGameEngine {
public enum SelectorStateID {
    UnActive,
    Activing,
    SelectOK,
    SelectCancel,
}
public class UISelectorInfo {

    protected Vector3 m_curPos;   //摇杆相对于摇杆中心点的坐标
    protected float m_rockerRange;  //摇杆圈圈的范围大小
    protected SelectorStateID m_state = SelectorStateID.UnActive;
    protected Vector3 startPoint3D; //3D空间内的起点
    float SelectObjAreaSize = 3f;
    public bool IsSimulation = false;

    public SelectorStateID SelectorState {
        get {
            return m_state;
        }
    }

    public Vector3 CurPos {
        get { return m_curPos; }
        set {
            Vector3 uiRockerPos = value;
            //UI2D（x,y,0）坐标到3D(x,0,z)的转换，再加上Camera视角导致的偏移
            uiRockerPos.z = uiRockerPos.y;
            uiRockerPos.y = 0;
            uiRockerPos.Normalize();
//             Transform playerCamera = BattleManager.Share().getMainCamera().transform;
//             Vector3 rawMoveDirection = playerCamera.forward * uiRockerPos.z + playerCamera.right * uiRockerPos.x;//Get movement stick
//             rawMoveDirection.y = 0;
//             m_curPos = rawMoveDirection.normalized;

            m_state = SelectorStateID.Activing;
        }
    }

    public UISelectorInfo(float rokerRange, Vector3 aStartPoint3D) {
        m_rockerRange = rokerRange;
        startPoint3D = aStartPoint3D;
        m_state = SelectorStateID.UnActive;
    }

    /// <summary>
    /// 结束选择。isOK true:成功选择目标  false:选择取消了
    /// </summary>
    /// <param name="isOK"></param>
    public void EndSelect(bool isOK) {
        m_state = isOK ? SelectorStateID.SelectOK : SelectorStateID.SelectCancel;
    }

    public virtual Vector3 GetDir() {
        return m_curPos;
    }

    /// <summary>
    /// 选取点与中间点的偏移。
    /// 真实空间中的偏移。已计算过真实和UI的比例。
    /// </summary>
    /// <param name="realRange">真实大小的范围，用于计算真实和UI比例</param>
    /// <returns></returns>
    public virtual Vector3 GetPosOffset(float realRange) {
        return m_curPos * (realRange / m_rockerRange);
    }

    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public virtual Puppet GetSelectedObj() {
        return ObjectManager.Share().GetObjectWithTouch(startPoint3D, SelectObjAreaSize);
    }

}
}
